﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using System;
using System.Collections;
using System.Collections.Generic;

namespace UtilPublic.Timer
{
    /// <summary>
    /// 定时器类型
    /// </summary>
    public enum ETimerType
    {
        /// <summary>
        /// 毫秒
        /// </summary>
        Millisecond = 0,
        /// <summary>
        /// 秒
        /// </summary>
        Second = 1,
        /// <summary>
        /// 分
        /// </summary>
        Minute = 2,
    }
    /// <summary>
    /// 定时器类
    /// </summary>
    public class MyTimer
    {
        /// <summary>
        /// 定时器ID
        /// </summary>
        public long Id;
        /// <summary>
        /// 定时器类型 0=秒定时器 1=分钟定时器
        /// </summary>
        public ETimerType Type = ETimerType.Millisecond;
        /// <summary>
        /// 定时器间隔
        /// </summary>
        public int Interval = 1;
        /// <summary>
        /// 定时器到期执行时间
        /// </summary>
        public DateTime ExecuteTime;
        /// <summary>
        /// 自动循环
        /// </summary>
        public bool AutoLoop = false;
        /// <summary>
        /// 定时器次数
        /// </summary>
        public int Amount = 1;
        /// <summary>
        /// 定时器事件
        /// </summary>
        public Action<object[]> TimerAction;
        /// <summary>
        /// 定时器时间参数
        /// </summary>
        public object[] ActionArgs;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MyTimer() { Id = ServerTimer.CreateId; }
        /// <summary>
        /// 设置执行时间
        /// </summary>
        public void SetTimer()
        {
            if (Type == ETimerType.Second)
            {
                ExecuteTime = DateTime.Now.AddSeconds(Interval);
            }
            else if (Type == ETimerType.Minute)
            {
                ExecuteTime = DateTime.Now.AddMinutes(Interval);
            }
            else
            {
                ExecuteTime = DateTime.Now.AddMilliseconds(Interval);
            }
        }
        /// <summary>
        /// 定时器执行
        /// </summary>
        public void ExecuteTimer()
        {
            TimerAction(ActionArgs);
        }
        /// <summary>
        /// 定时器开始
        /// </summary>
        /// <returns></returns>
        public long Start()
        {
            return ServerTimer.TimerStart(this);
        }
        /// <summary>
        /// 定时器结束
        /// </summary>
        public void Stop()
        {
            ServerTimer.TimerStop(this.Id);
        }
    }
}